package consumer;

import user.list.SynchronizedBuffer;;

public class Consumer<T> extends Thread {

	private final int id;
	private SynchronizedBuffer<T> buffer;
	private final int timeBetweenConsumptions;

	public Consumer(int id, SynchronizedBuffer<T> buffer, int timeBetweenConsumptions) {
		this.id = id;
		this.buffer = buffer;
		this.timeBetweenConsumptions = timeBetweenConsumptions;
	}

	private boolean doSleep() {
		try {
			Thread.sleep(this.timeBetweenConsumptions);
		} catch (InterruptedException e) {
			return false;
		}
		return true;
	}

	@Override
	public void run() {
		while (doSleep()) {
			try {
				System.out.println("consumer nr." + String.format("%3d", id) + " consumes: \t" + this.buffer.interruptablePop().toString());
				System.out.flush();
			} catch (InterruptedException e) {
				return;
			}
		}
	}

}
